package com.hxkcloud.patient.dborm;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.table.TableUtils;
import com.hxkcloud.patient.beans.DiseaseBean;
import com.hxkcloud.patient.beans.DiseaseWholeBean;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by william_sun on 2016-07-22.
 * Description:病种列表数据库操作类
 */
public class DiseaseDao {
    private Context context;
    private Dao<DiseaseBean, Integer> diseaseBeanDao;

    public DiseaseDao(Context context) {
        this.context = context;
        try {
            diseaseBeanDao = DatabaseHelper.getHelper(context).getDao(DiseaseBean.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    //-1 chanke
    public List<DiseaseBean> getDisease(String departmentId) {
        try {
            QueryBuilder builder = diseaseBeanDao.queryBuilder();
            builder.where().eq("departmentId", departmentId);
            return builder.query();
//            return diseaseBeanDao.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void setDisease(DiseaseWholeBean diseases) {
        try {
            clearDisease();
            for (DiseaseBean d : diseases.getList()) {
                diseaseBeanDao.create(d);
            }
            for (DiseaseBean d : diseases.getChankeList()) {
                d.setDepartmentId("-1");
                diseaseBeanDao.create(d);
            }
        } catch (SQLException e) {
        }
    }

    public void clearDisease() {
        try {
            TableUtils.clearTable(diseaseBeanDao.getConnectionSource(), DiseaseBean.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
